package com.gs.cd.service.auth.controller;


import com.gs.cd.api.ApiResult;
import com.gs.cd.eum.CategoryEnum;
import com.gs.cd.model.resource.SysResourceGroup;
import com.gs.cd.service.auth.model.AuthUser;
import com.gs.cd.service.auth.model.vo.AuthUserVo;
import com.gs.cd.service.auth.service.AuthUserRoleService;
import com.gs.cd.service.auth.service.AuthUserService;
import com.gs.cd.service.resource.api.ResourceApi;
import com.gs.cd.service.role.model.AuthRole;
import com.gs.cd.service.role.model.AuthRoleResourceGroup;
import com.gs.cd.service.role.service.AuthRoleResourceGroupService;
import com.seven.mybatisx.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户管理
 *
 * @author seven
 * @since 2020-12-22
 */
@RestController
@RequestMapping("/auth/authUser")
public class AuthUserController extends AbstractController<AuthUser, AuthUserService> {

    @Autowired
    private AuthUserService authUserService;
    @Autowired
    private AuthUserRoleService authUserRoleService;
    @Autowired
    private AuthRoleResourceGroupService authRoleResourceGroupService;
    @Autowired
    private ResourceApi resourceApi;

    /**
     * 获取用户拥有的角色
     */
    @GetMapping("/list/allRole")
    public ApiResult listAllRole(String loginName
            , @RequestParam(defaultValue = "default", required = false) String tenantCode) {
        // TODO: 2020/12/22 以后通过token来获取
        AuthUserVo authUserVo = authUserService.listWithRole(loginName, tenantCode);
        return ApiResult.success(authUserVo);
    }

    /**
     * 获取用户拥有的所有资源
     * 分层了
     */
    @GetMapping("list/allResource")
    public ApiResult listAllResource(String loginName
            , @RequestParam(defaultValue = "default", required = false) String tenantCode) {
        // TODO: 2020/12/22 以后通过token来获取
        AuthUserVo authUserVo = authUserService.listWithRole(loginName, tenantCode);
        Set<String> collect = authUserVo.getAuthRoleList().stream().map(AuthRole::getRoleCode).collect(Collectors.toSet());
        //获取角色拥有的资源组code
        Set<String> resourceGroupCodeList = authRoleResourceGroupService.listByRoleCodes(collect, tenantCode).stream().map(AuthRoleResourceGroup::getResourceGroupCode).collect(Collectors.toSet());
        List<SysResourceGroup> list = resourceApi.resourceByGroupCodes(resourceGroupCodeList);
        //资源分层
        HashMap<CategoryEnum, SysResourceGroup> re = new HashMap<>();
        list.forEach(sysResourceGroup -> {
            CategoryEnum category = sysResourceGroup.getCategory();
            re.put(category, sysResourceGroup);
        });
        return ApiResult.success(re);
    }



}

